package com.gs.m_home.persistent

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.constant.StateCode
import com.gs.m_home.callback.OnDoAddGoodSoldCountStateCallback
import com.gs.m_home.callback.OnDoDeleteGoodStateCallback
import com.gs.m_home.callback.OnDoPayOrderStateCallback
import com.gs.m_home.callback.OnDoSubtractGoodSoldCountStateCallback
import com.gs.m_home.callback.OnDoUpdateCartGoodDataStateCallback
import com.gs.m_home.callback.OnGetCartOrderIdStateCallback
import com.gs.m_home.model.CartModel

class CartPersistent(context: Context) {

    private val cartModel by lazy { CartModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun updateCartGoodData(username: String, callback: OnDoUpdateCartGoodDataStateCallback) {
        Thread{
            cartModel.updateCartGoodData(username) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onUpdateCartGoodDataLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onUpdateCartGoodDataSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onUpdateCartGoodDataFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun addGoodSoldCount(orderId: Int, goodId: Int, callback: OnDoAddGoodSoldCountStateCallback) {
        Thread{
            cartModel.addGoodSoldCount(orderId, goodId) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onAddGoodSoldCountLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onAddGoodSoldCountSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onAddGoodSoldCountFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun subtractGoodSoldCount(orderId: Int, goodId: Int, callback: OnDoSubtractGoodSoldCountStateCallback) {
        Thread{
            cartModel.subtractGoodSoldCount(orderId, goodId) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onSubtractGoodSoldCountLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onSubtractGoodSoldCountSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onSubtractGoodSoldCountFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun deleteGood(orderId: Int, goodId: Int, callback: OnDoDeleteGoodStateCallback) {
        Thread{
            cartModel.deleteGood(orderId, goodId) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onDeleteGoodLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onDeleteGoodSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onDeleteGoodFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun payOrder(username: String, callback: OnDoPayOrderStateCallback) {
        Thread{
            cartModel.payOrder(username){
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onPayOrderLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onPayOrderSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onPayOrderFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun getCartOrderId(username: String, callback: OnGetCartOrderIdStateCallback) {
        Thread{
            cartModel.getCartOrderId(username){
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetCartOrderIdLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetCartOrderIdSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetCartOrderIdFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    companion object{
        private var instance: CartPersistent? = null
        fun getInstance(context: Context): CartPersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = CartPersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}